/**
 * @Author wywinstonwy
 * @Date 2021/12/27 9:50 下午
 * @Description:
 */
import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';

class WyPageView extends StatefulWidget {
  const WyPageView({Key? key}) : super(key: key);

  @override
  _WyPageViewState createState() => _WyPageViewState();
}

class _WyPageViewState extends State<WyPageView> {
  int current = 1;
  int _currentPageIndex = 0;
  List<Widget> pageList = [ Container(color: Colors.red,),
    Container(color: Colors.black,),
    Container(color: Colors.yellow,),];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar('PageView'),
      body: current==0?_createPageView():_buildPageView(),
    );
  }

  _createPageView(){
    return Container(

      height:200 ,
      child: PageView(

        scrollDirection: Axis.horizontal,
        controller: PageController(viewportFraction: 0.9,initialPage: 1),
        onPageChanged: (int index){
          print(index);
        },
        children: [
          Container(color: Colors.red,),
          Container(color: Colors.black,),
          Container(color: Colors.yellow,),
        ],
      ),
    );
  }
  _createPageView1(){
    return Container(

      height:200 ,
      child: PageView.builder(
          itemCount: 1000,
          itemBuilder: (context,index){
            return pageList[index%(pageList.length)];

      }),
    );
  }
  _buildPageView(){
    return Center(
      child: Container(
        height: 230,
        child: Stack(
          children: [
            PageView.builder(
              allowImplicitScrolling: true,
                padEnds: true,

                itemBuilder: (context,index){
              var val = pageList[index%(pageList.length)];
              print('build $index');
              return _buildPageViewItem('${val}');
            }),
            Positioned(
                bottom: 20,
                left: 0,
                right: 0,
                child: Container(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: List.generate(pageList.length, (i){
                      return Container(
                        margin: EdgeInsets.symmetric(horizontal: 5),
                        width: 10,
                        height: 10,
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: _currentPageIndex==i?Colors.blue:Colors.grey
                        ),
                      );
                    }).toList(),
                  ),
                )
            )
          ],
        ),
      ),
    );
  }
  _buildPageViewItem(String txt,{Color color=Colors.red}){
    return Container(
      color: color,
      alignment: Alignment.center,
      child: Text(txt,style: TextStyle(color: Colors.white,fontSize: 25),),
    );
  }
}
